package com.joplus.housekeeper.utils;

import com.joplus.housekeeper.bean.PushMessageBean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/7/28.
 */
public class SerializableHashMap {


    public static String serialize(HashMap<String, Object> hashMap) {

        try {
            ByteArrayOutputStream mem_out = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(mem_out);
            out.writeObject(hashMap);
            out.close();
            mem_out.close();
            byte[] bytes = mem_out.toByteArray();
            return new String(bytes);
        } catch (IOException e) {
            return null;
        }
    }



    public static HashMap<String, PushMessageBean> deserialize(String s) {
        try {
            byte[] bytes=s.getBytes();
            ByteArrayInputStream mem_in = new ByteArrayInputStream(bytes);
            ObjectInputStream in = new ObjectInputStream(mem_in);
            HashMap<String, PushMessageBean> hashMap = (HashMap<String, PushMessageBean>) in.readObject();
            in.close();
            mem_in.close();
            return hashMap;
        } catch (StreamCorruptedException e) {
            return null;
        } catch (ClassNotFoundException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }

}
